﻿using BP.En;
using Microsoft.AspNetCore.Server.IIS.Core;
using Renci.SshNet.Security;
using System;
using System.Data;

namespace BP.Sys.Printer
{
    /// <summary>
    /// 打印数据
    /// </summary>
    public class DB
    {
        /// <summary>
        /// 数据
        /// </summary>
        public DBEles HisDBEles = null;
        /// <summary>
        /// 从表数据源
        /// </summary>
        public DBDtls HisDBDtls = null;
        /// <summary>
        /// 打印数据
        /// </summary>
        public DB(string frmID, string frmName, string templateID, Int64 workID)
        {
            //所有数据源.
            PTDBSrcs srcs = new PTDBSrcs(templateID); //数据源集合,包含宿主数据源.
            GEEntity en = new GEEntity(frmID, workID); //获得宿主 - 主表数据,为其他数据源提供参数.

            #region 1.生成主表数据源.
            this.HisDBEles = new DBEles();
            foreach (PTDBSrc src in srcs)
            {
                //如果是查询.
                if (src.DBTypeID.Equals("Search") == true)
                {
                    SFSearch sFSearch = new SFSearch(src.RefFrmID);
                    if (sFSearch.ResultNum == 0)
                        continue; //如果是返回多行数据的.

                    //加入到集合里.
                    DataTable dt = sFSearch.GenerHisDataTable();
                    if (dt.Rows.Count != 1)
                        throw new Exception("err@查询[" + sFSearch.Name + "，没有获得一行数据，请检查该配置项，为查询提供的参数:" + en.ToJson());
                    foreach (DataColumn dc in dt.Columns)
                    {
                        this.HisDBEles.Add(dc.ColumnName, dc.ColumnName, dt.Rows[0][dc.ColumnName].ToString(), 1, src.RefFrmID);
                    }
                    continue;
                }
                //单据与实体.
                GEEntity myen = null;
                if (src.RefFrmID.Equals(frmID) == true)
                    myen = en; //如果是宿主实体.
                else
                {
                    //不需要关联的.
                    if (src.FrmRefPKModel == 0)
                    {
                        myen = new GEEntity(src.RefFrmID);
                        myen.PKVal = workID;
                        if (myen.RetrieveFromDBSources() == 0)
                            throw new Exception("err@请检查关联的单据数据源是否正确,单据[" + src.RefFrmID + "," + src.RefFrmName + "],打印主键：" + workID);
                    }

                    if (src.FrmRefPKModel == 1)
                    {
                        string pkval = en.GetValStringByKey(src.FrmRefPKAttrKey); ////从宿主实体里，求实体主键
                        if (BP.DA.DataType.IsNullOrEmpty(pkval) == true)
                            throw new Exception("err@获取实体数据源["+src.RefFrmID+","+src.RefFrmName+"]出现错误,没有从宿主数据源中获得实体主键.");
                        myen = new GEEntity(src.RefFrmID);
                        myen.PKVal = pkval;
                        if (myen.RetrieveFromDBSources() == 0)
                            throw new Exception("err@请检查关联的单据数据源是否正确，没有获得数据,关联宿主主键["+pkval+"]字段["+src.FrmRefPKAttrKey+"],单据[" + src.RefFrmID + "," + src.RefFrmName + "]。" );
                    }

                }
                //遍历实体属性.
                foreach (Attr attr in myen.EnMap.Attrs)
                {
                    if (attr.MyFieldType == FieldType.Enum)
                    {
                        this.HisDBEles.Add(attr.Key, attr.Desc, myen.GetValRefTextByKey(attr.Key), attr.MyDataType, src.RefFrmID);
                        continue;
                    }

                    //if (attr.UIContralType == UIContralType.DDL &&  attr.UIBindKey == FieldType.Enum)
                    //{
                    //    this.HisDBEles.Add(attr.Key, attr.Desc, myen.GetValRefTextByKey(attr.Key), attr.MyDataType, src.RefFrmID);
                    //    continue;
                    //}

                    if (attr.MyDataType == BP.DA.DataType.AppBoolean)
                    {
                        if (myen.GetValIntByKey(attr.Key) == 1)
                            this.HisDBEles.Add(attr.Key, attr.Desc, "是", attr.MyDataType, src.RefFrmID);
                        else
                            this.HisDBEles.Add(attr.Key, attr.Desc, "否", attr.MyDataType, src.RefFrmID);
                        continue;
                    }

                    //其他类型.
                    this.HisDBEles.Add(attr.Key, attr.Desc, myen.GetValByKey(attr.Key), attr.MyDataType, src.RefFrmID);
                }
            }
            #endregion 生成主表数据源.

            #region 2.生成从表数据源.
            this.HisDBDtls = new DBDtls();
            foreach (PTDBSrc src in srcs)
            {
                //如果是查询.
                if (src.DBTypeID.Equals("Search") == true)
                {
                    SFSearch sFSearch = new SFSearch(src.RefFrmID);
                    if (sFSearch.ResultNum == 1)
                        continue; //如果是返回单行数据的.

                    //加入到集合里.
                    DBDtl dtl = new DBDtl();
                    dtl.No = src.RefFrmID;
                    dtl.Name = src.RefFrmName;
                    dtl.HisDB = sFSearch.GenerHisDataTable(en.Row);
                    this.HisDBDtls.Add(dtl);
                    continue;
                }

                MapDtls dtls = new MapDtls(src.RefFrmID);
                foreach (MapDtl dtl in dtls)
                {
                    GEDtls gEDtls = null;
                    //如果是宿主实体.
                    if (src.RefFrmID.Equals(frmID) == true)
                    {
                        gEDtls = new GEDtls(dtl.No, workID);
                    }
                    else
                    {
                        gEDtls = new GEDtls(dtl.No, int.Parse(this.HisDBEles.GetValByKey(src.FrmID, src.FrmRefPKAttrKey).AttrValue.ToString()));
                    }
                    //加入到集合里.
                    DBDtl mydtl = new DBDtl();
                    mydtl.No = dtl.No;
                    mydtl.Name = dtl.Alias;
                    mydtl.HisDB = gEDtls.ToDataTableField(); // sFSearch.GenerHisDataTable(en.Row);
                    this.HisDBDtls.Add(mydtl);
                    // this.HisDBEles.Add(key, key, myen.GetValByKey(key), 1, src.RefFrmID);
                }
            }
            #endregion 生成主表数据源.
        }
    }

}
